/**
 * 
 */
package org.log4jconfig.xml.editor.contentassist.attribute;

import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.xml.core.internal.parser.regions.AttributeValueRegion;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.log4jconfig.xml.editor.contentassist.IAttributeContentAssistComputer;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public abstract class AbstractContentAssistComputer implements IAttributeContentAssistComputer {

	protected void addCompletionProposal(ContentAssistRequest contentAssistRequest, String proposalValue,
			String attributeCurrentValue, String attributeMatchValue, String imageKey) {
		addCompletionProposal(contentAssistRequest, proposalValue, null, attributeCurrentValue, attributeMatchValue,
				imageKey);
	}

	protected void addCompletionProposal(ContentAssistRequest contentAssistRequest, String proposalValue,
			String javadoc, String attributeCurrentValue, String attributeMatchValue, String imageKey) {
		// FIXME : what if current document region is AttributeEqualsRegion : no
		// content assist ?
		if (proposalValue.toLowerCase().indexOf(attributeMatchValue.toLowerCase()) == 0
				&& contentAssistRequest.getRegion() instanceof AttributeValueRegion) {
			String replacementValue = new StringBuilder().append('\"').append(proposalValue).append("\"").toString();
			int replacementOffset = contentAssistRequest.getStartOffset();
			int replacementLength = contentAssistRequest.getRegion().getTextLength();
			Image image = JavaUI.getSharedImages().getImage(imageKey);
			ICompletionProposal proposal = new CompletionProposal(replacementValue, replacementOffset,
					replacementLength, replacementValue.length() - 1, image, proposalValue, null, javadoc);
			contentAssistRequest.addProposal(proposal);
		}

	}
}
